﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace lchealthy
{
    public partial class frmCustomerShow : Form
    {
        public frmCustomerShow()
        {
            InitializeComponent();
        }
        DataSet ds;
        private void frmCustomerShow_Load(object sender, EventArgs e)
        {
            custBind();
            
           
        }
        private void custBind()
        {
            string sql="";
            if(!btnBuy.Visible)
            sql = "SELECT customer.*,(year(date())-year(customer.cbirthday)) as age, dietitianinfo.dName as yys  FROM dietitianinfo INNER JOIN customer ON dietitianinfo.dID = customer.did";
            else if(!userinfo.isadmin)
                sql = "SELECT customer.*,(year(date())-year(customer.cbirthday)) as age, dietitianinfo.dName as yys  FROM dietitianinfo INNER JOIN customer ON dietitianinfo.dID = customer.did where customer.did=" + userinfo.id;
            else
                sql = "SELECT customer.*,(year(date())-year(customer.cbirthday)) as age, dietitianinfo.dName as yys  FROM dietitianinfo INNER JOIN customer ON dietitianinfo.dID = customer.did";
            ds = DbHelperOleDb.Query(sql);
            dgvCustomer.AutoGenerateColumns = false;
            dgvCustomer.DataSource = ds.Tables[0];
        }
        //根据客户姓名和姓名拼音查找客户信息
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string cName = txtName.Text;
            string sql = "SELECT  customer.*,(year(date())-year(customer.cbirthday)) as age, dietitianinfo.dName as yys  FROM dietitianinfo INNER JOIN customer ON dietitianinfo.dID = customer.did where 1=1  ";
            if (!string.IsNullOrEmpty(cName))
            {
                sql+= "and cname like '" + cName + "%'";
            }
            string cPinYin = txtPinYin.Text;
            cPinYin = cPinYin.ToUpper();//把用户输入的字母转换成大写字母
            if (!string.IsNullOrEmpty(cPinYin))
            {
              sql+= "and cpinyin like '%"+cPinYin+ "%'";
            }
            ds = DbHelperOleDb.Query(sql);
            dgvCustomer.DataSource = ds.Tables[0];
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            int row = -1;
            row = dgvCustomer.CurrentRow.Index;
           
             int cid = int.Parse(ds.Tables[0].Rows[row]["cid"].ToString()); 
           
            frmCustomer f = new frmCustomer(cid);
            f.Text = "客户信息修改";
            f.ShowDialog();
            custBind();             

        }

        private void btmDel_Click(object sender, EventArgs e)
        {
            int row = dgvCustomer.CurrentRow.Index;
             int cid = int.Parse(ds.Tables[0].Rows[row]["cid"].ToString());
             string sql;
             sql = "delete from customer where cid=" + cid;
             DialogResult n = MessageBox.Show("你确定要删除吗？", "提示！", MessageBoxButtons.YesNo);
             if (n == DialogResult.Yes)
             {
                 int i = DbHelperOleDb.ExecuteSql(sql);
                 if (i == 1)
                 {
                     MessageBox.Show("删除成功！");
                     custBind();
                 }
                 else 
                 {
                     MessageBox.Show("删除失败！");
                 }
             }
        }

        private void btnshow_Click(object sender, EventArgs e)
        {
            int row = dgvCustomer.CurrentRow.Index;
            int cid = int.Parse(ds.Tables[0].Rows[row]["cid"].ToString());
            frmtestlist f = new frmtestlist(cid);
            f.MdiParent = this.MdiParent;
            f.Show();


        }

        private void btnBuy_Click(object sender, EventArgs e)
        {
            int row = dgvCustomer.CurrentRow.Index;
            int cid = int.Parse(ds.Tables[0].Rows[row]["cid"].ToString());
            frmCustomerBuy f = new frmCustomerBuy(cid,1);          
            f.Text = DbHelperOleDb.GetSingle("select cname from customer where cid=" + cid).ToString() + "购买产品！";
            f.Show();
        }

        private void buylistToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int row = dgvCustomer.CurrentRow.Index;
            int cid = int.Parse(ds.Tables[0].Rows[row]["cid"].ToString());
            frmbuylist f = new frmbuylist(cid);
            f.Show();

        }

        private void btnDeposit_Click(object sender, EventArgs e)
        {
            int row = dgvCustomer.CurrentRow.Index;
            int cid = int.Parse(ds.Tables[0].Rows[row]["cid"].ToString());
            //frmCustomerBuy f = new frmCustomerBuy(cid,2);
            //f.Text = DbHelperOleDb.GetSingle("select cname from customer where cid=" + cid).ToString() + "寄存产品！";
            //f.Show();
            frmdeposit f = new frmdeposit(cid);
            f.Show();
        }

        private void btnBuyRecord_Click(object sender, EventArgs e)
        {
            int row = dgvCustomer.CurrentRow.Index;
            int cid = int.Parse(ds.Tables[0].Rows[row]["cid"].ToString());
            frmbuylist f = new frmbuylist(cid);
            f.Show();
        }

        private void txtName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btnSearch_Click(null, null);
            }
        }

        private void txtPinYin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btnSearch_Click(null, null);
            }
        }

        private void btnmy_Click(object sender, EventArgs e)
        {
            if (btnmy.Text == "我的客户")
            {
                btnBuy.Visible = true;
                btnDeposit.Visible = true;
                btnModify.Visible = true;
                btnBuyRecord.Visible = true;
                btmDel.Visible = true;
                btnmy.Text = "所有客户";
                custBind();
            }
            else
            {
                btnBuy.Visible = false;
                btnDeposit.Visible = false;
                btnModify.Visible = false;
                btnBuyRecord.Visible = false;
                btmDel.Visible = false;
                btnmy.Text = "我的客户";
                custBind();
            }


        }

        private void btninshop_Click(object sender, EventArgs e)
        {
            int row = dgvCustomer.CurrentRow.Index;
            int cid = int.Parse(ds.Tables[0].Rows[row]["cid"].ToString());
            frminshops f = new frminshops(cid);
            f.Show();

        }

        private void 积分ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int row = dgvCustomer.CurrentRow.Index;
            int cid = int.Parse(ds.Tables[0].Rows[row]["cid"].ToString());
            frmjf f = new frmjf(cid);
            f.Show();
        }

        private void 消费ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int row = dgvCustomer.CurrentRow.Index;
            int cid = int.Parse(ds.Tables[0].Rows[row]["cid"].ToString());
            frminshop f = new frminshop(cid);
            f.Show();
        }

        private void 寄存ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            int row = dgvCustomer.CurrentRow.Index;
            int cid = int.Parse(ds.Tables[0].Rows[row]["cid"].ToString());
            frmCustomerBuy f = new frmCustomerBuy(cid, 2);
            f.Text = DbHelperOleDb.GetSingle("select cname from customer where cid=" + cid).ToString() + "寄存产品！";
            f.Show();            
        }


       

      
       

        
      

      
    }
}
